package com.sskj.lsgd.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.lsgd.domain.vo.EquipMent;
import com.sskj.utils.HttpUtils;

@RestController
@RequestMapping("/wechat/lsgd")
public class LvSeGongDiServiceImpl {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Value("${sskj.conf.ycUrl}")
	private String ycUrl;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/queryProjectOrder", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "加载绿色工地main工程所需数据")
	public String queryProjectOrder(EquipMent equipment) {
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("city", equipment.getCity());
		querys.put("cityCode", equipment.getCityCode());
		querys.put("orderElement", equipment.getOrderElement());
		querys.put("orderDeriction", equipment.getOrderDeriction());
		String path = "/weiQueryProjectOrder.do";
		return this.returnResult(path, querys);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/queryWeatherInfo", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "加载绿色工地main天气状况数据")
	public String queryWeatherInfo(String city) {
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("city", city);
		String path = "/weiQueryWeatherInfo.do";
		return this.returnResult(path, querys);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/seachConstruction", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "搜索工程列表")
	public String seachConstruction(String pname) {
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("pname", pname);
		String path = "/queryAllConstruction.do";
		return this.returnResult(path, querys);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/equipmentDetail", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询工程对应的工程详情")
	public String equipmentDetail(String pcode) {
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("pcode", pcode);
		String path = "/weiGetEquipMentDetail.do";
		return this.returnResult(path, querys);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/queryAvgByDistrict", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据城市用区域排名pm2.5和噪音")
	public String queryAvgByDistrict(String city) {
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("city", city);
		String path = "/weiQueryAvgByDistrict.do";
		return this.returnResult(path, querys);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/querypmapByCity", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据城市查询当地的工程位置信息")
	public String querypmapByCity(String city) {
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("city", city);
		String path = "/weiQuerypmapByCity.do";
		return this.returnResult(path, querys);
	}

	private String returnResult(String path, Map<String, String> querys) {
		try {

			String method = "GET";
			Map<String, String> headers = new HashMap<String, String>();
			HttpResponse response = HttpUtils.doGet(ycUrl, path, method, headers, querys);

			if (response.getStatusLine().getStatusCode() != 200) {
				logger.error(
						"request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + ycUrl);
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String resultStr = EntityUtils.toString(entity, "utf-8");
				return resultStr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
